package com.bz.xtcx.manager.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bz.xtcx.manager.utils.WzStrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.bz.xtcx.manager.entity.CmsFile;
import com.bz.xtcx.manager.service.ICmsFileService;
import com.bz.xtcx.manager.service.ILibImageService;
import com.bz.xtcx.manager.utils.MyFileUtil;
import com.bz.xtcx.manager.vo.VoResponse;

@RestController
@RequestMapping("file")
public class FileUploadController {
	
	@Autowired
	private ILibImageService userImageService;
	
	@Autowired
	private ICmsFileService cmsFileService;
	
	@Value("${file.tomcatPath}")
    private String tomcatPath;
	
	@DeleteMapping
	public Object del(@RequestParam("id") String id) {
		VoResponse voRes = new VoResponse();
		if(!cmsFileService.del(id)) {
			voRes.setFail(voRes);
		}
		return voRes;
	}

	/**
	 * 上传封面 ，栏目，内容，活动
	 * @param file
	 * @return
	 */
	@PostMapping("upload")
	public Object upload(@RequestParam("file")MultipartFile file) {
		MyFileUtil util = new MyFileUtil();
		String path = "cover/";
		String filePath = tomcatPath + path;
		VoResponse voRes = null;
		String fileName = file.getOriginalFilename();
		String [] suffixNames = {".jpg", ".jpeg", ".png"};
		voRes = util.upload(file, filePath, suffixNames);
		if(voRes.getSuccess()) {
			CmsFile e = new CmsFile();
			e.setFileName(fileName);
			e.setReqRelUrl(voRes.getData().toString());
			String sp = path + voRes.getData();
			if (WzStrUtil.isNotBlank(sp) && sp.startsWith("/")) {
			    sp = sp.substring(1);
            }
			e.setSavePath(sp);
			voRes.setData(e);
		}
		return voRes;
	}
	
	// 文件下载相关代码
	@GetMapping("/download")
	public Object downloadFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileName") String fileName) {
		//String fileName = "readme.txt";
		VoResponse voRes = new VoResponse();
		if (fileName != null) {
			// 当前是从该工程的WEB-INF//File//下获取文件(该目录可以在下面一行代码配置)然后下载到C:\\users\\downloads即本机的默认下载的目录
			//String realPath = request.getServletContext().getRealPath("//WEB-INF//");
			//String realPath = "D://test//";
			String realPath = "/root/java/xtcx/";
			File file = new File(realPath, fileName);
			if (file.exists()) {
			    response.setCharacterEncoding("utf-8");
			    response.setContentType("multipart/form-data");
			    response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
			    try {
			        //打开本地文件流
			        InputStream inputStream = new FileInputStream(realPath + fileName);
			        //激活下载操作
			        OutputStream os = response.getOutputStream();
			        //循环写入输出流
			        byte[] b = new byte[2048];
			        int length;
			        while ((length = inputStream.read(b)) > 0) {
			            os.write(b, 0, length);
			        }
			        // 这里主要关闭。
			        os.close();
			        inputStream.close();
			        return null;
			    }catch (Exception e){
			        e.printStackTrace();
			    }
			}
		}
		return voRes;
	}

	// 多文件上传
	@PostMapping("batch/upload")
	public String handleFileUpload(HttpServletRequest request) {
		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
		MultipartFile file = null;
		BufferedOutputStream stream = null;
		for (int i = 0; i < files.size(); ++i) {
			file = files.get(i);
			if (!file.isEmpty()) {
				try {
					byte[] bytes = file.getBytes();
					stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
					stream.write(bytes);
					stream.close();

				} catch (Exception e) {
					stream = null;
					return "You failed to upload " + i + " => " + e.getMessage();
				}
			} else {
				return "You failed to upload " + i + " because the file was empty.";
			}
		}
		return "upload successful";
	}

	public static void main(String[] args) {
	}
}
